import axios from 'axios';
import store from '@/store'
import {Message} from 'element-ui'

// 统一请求
const axiosReq=axios.create({
    // baseURL:"http://43.142.56.225:8686",
    baseURL:"http://localhost:8686",
    timeout:10000
})

//请求拦截器 每次请求时都加入token
axiosReq.interceptors.request.use(config => {
    if (store.state.token) {
        config.headers['Authorization'] = localStorage.token
    }
    return config;
}, error => {
   return Promise.reject(error);
})

//响应拦截器
axiosReq.interceptors.response.use(response => {
    const res = response.data;
    if (res.code !== 200) {
        //用户未登录
        if (res.code === 201) {
            Message({
                type: 'warning',
                showClose: true,
                message: '登录超时'
            })
            return Promise.reject('error');
        }

        //系统异常报错
       /* if (res.code === 500) {
            alert('h')
            Message({
                type: 'warning',
                showClose: true,
                message: '系统异常，请稍后再试'
            })
            return Promise.reject('error');
        }*/

        return Promise.reject(res.msg);
    }
    return response;
    }, error => {
        return Promise.reject('error')
    })
export default axiosReq
